﻿using System;

namespace Sencha.Serialization.Bson
{

    /// <summary>
    /// Representation of symbol in BSON. This type is immutable by design
    /// </summary>
	[TypeSerializer(typeof(Serializers.BsonSymbolSerializer))]
	public class BsonSymbol : IEquatable<BsonSymbol>
    {
		private string symbol;

		public string Raw { get { return symbol; } }

		public BsonSymbol()
		{
			this.symbol = "";
		}
        public BsonSymbol(string symbol)
        {
			if (symbol == null)
				throw new ArgumentNullException("symbol");

            this.symbol = symbol;
        }

		#region IEquatable<BsonSymbol> Members

		public override bool Equals(object obj)
		{
			return this.Equals(obj as BsonSymbol);
		}

		public override int GetHashCode()
		{
			return this.symbol.GetHashCode();
		}

		public bool Equals(BsonSymbol other)
		{
			if (other == null)
				return false;

			return this.symbol.Equals(other.symbol);
		}

		#endregion

		public static bool operator ==(BsonSymbol a, BsonSymbol b)
		{
			if (ReferenceEquals(a, null) && ReferenceEquals(b, null))
				return false;
			else if (ReferenceEquals(a, null) || ReferenceEquals(b, null))
				return false;
			else
				return a.Equals(b);

		}
		public static bool operator !=(BsonSymbol a, BsonSymbol b)
		{
			return !(a == b);
		}
		public static implicit operator BsonSymbol(string symbol)
		{
			if (symbol == null)
				return null;

			return new BsonSymbol(symbol);
		}
		public static explicit operator string(BsonSymbol symbol)
		{
			if (symbol == null)
				return "";

			return symbol.symbol;
		}

        public override string ToString()
        {
			return this.symbol;
        }
    }
}
